from ruamel import yaml

class YamlLoader:

    def __init__(self, file):
        self.file = file
    # 读yaml文件
    def file_load(self):
        with open(self.file, 'r', encoding='utf-8') as f:
            data = f.read()
        return yaml.load(data, Loader=yaml.RoundTripLoader)

    def file_dump(self, data):
        with  open(self.file, 'w', encoding='utf-8') as f:
            yaml.dump(data, f, Dumper=yaml.RoundTripDumper,default_flow_style=False,encoding='utf-8',allow_unicode=True)

if __name__ == '__main__':
    import os
    from ruamel import yaml

    # 作者：上海-悠悠 QQ交流群：330467341

    # 将字典写入到yaml
    desired_caps = {
        'platformName': 'Android',
        'platformVersion': '7.0',
        'deviceName': 'A5RNW18316011440',
        'appPackage': 'com.tencent.mm',
        'appActivity': '.ui.LauncherUI',
        'automationName': 'Uiautomator2',
        'unicodeKeyboard': True,
        'resetKeyboard': True,
        'noReset': True,
        'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
    }

    curpath = os.path.dirname(os.path.realpath(__file__))
    yamlpath = os.path.join(curpath, "caps.yaml")

    # 写入到yaml文件
    with open(yamlpath, "w", encoding="utf-8") as f:
        yaml.dump(desired_caps, f, Dumper=yaml.RoundTripDumper)

    file = YamlLoader('caps.yaml')
    d = file.file_load()
    print(d)
